<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/8/24
 * Time: 11:53 上午
 */

namespace App\Models\CacheModel\live;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * Class LiveGiftSendRecord
 * @package App\Models\CacheModel\live
 */
class LiveGiftSendRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_gift_send_record';
    protected $cacheModel = 'live.gift_send_record';

    /**
     * 添加礼物赠送记录
     * @param $uid
     * @param $type
     * @param $data
     * @throws \Exception
     */
    public function saveData($uid, $type, $data)
    {
        $id = $this->insertData($data);
        $this->listDataAdd($uid, [$id => $id], $type);
    }

    /**
     * 获取礼物赠送记录id
     * @param $type
     * @param $uid
     * @return array|mixed
     * @throws \Exception
     */
    public function giftSendRecord($uid, $type)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($type)->ZsGetIocData('list', function () use ($type, $uid)
        {
            try
            {
                $ids = $this->where(['type' => $type, 'uid' => $uid])->column('id', 'id');
                $ids = array_flip_key_value($ids);
            }
            catch (\Exception $e)
            {
                $ids = [];
            }
            return $ids;
        });
    }

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this
                ->alias('lr')
                ->join('wechat_backup_07_18_14_43_00.live_gift g', 'lr.gid=g.id', 'LEFT')
                ->join('wechat_backup_07_18_14_43_00.live_anchor_info a', 'lr.live_id=a.live_id', 'LEFT')
                ->where(['lr.id' => ['in', $ids]])
                ->column('lr.*,g.name gname,a.name,a.avatar', 'lr.id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    /**
     * 获取直播间礼物赠送数据
     * @param $liveRecordIds
     * @return array
     */
    public function getLiveRecordGiftData($liveRecordIds)
    {
        $sendGiftData = $this->whereIn('live_record_id', $liveRecordIds)->group('gid')->column('sum(send_num) num', 'gid');

        $giftIds = array_keys($sendGiftData);

        $giftInfos = (new LiveGift())->whereIn('id', $giftIds)->column('type,name,coin', 'id');

        $resData = [
            'type1' => '',
            'type2' => [
                'coin' => 0,
                'desc' => ''
            ],
            'type3' => [
                'coin' => 0,
                'desc' => ''
            ],
        ];
        foreach ($sendGiftData as $gid => $num)
        {
            if (isset($giftInfos[$gid]))
            {
                if ($giftInfos[$gid]['type'] == 1)
                {   // 免费礼物
                    $resData['type1'] .= "{$giftInfos[$gid]['name']}x{$num}、";
                } else
                {   // 付费礼物

                    $resData['type2']['coin'] += $num * $giftInfos[$gid]['coin'];
                    $resData['type2']['desc'] .= "{$giftInfos[$gid]['name']}x{$num}、";
                }
            }
        }

        $BagSendGiftData = $this->alias('r')->join('wechat_backup_07_18_14_43_00.live_gift g', 'r.gid=g.id')->whereIn('r.live_record_id', $liveRecordIds)
            ->where('g.type', 2)->where('r.type', 2)->group('r.gid')->column('sum(r.send_num) num', 'r.gid');

        foreach ($BagSendGiftData as $gid => $num)
        {
            if (isset($giftInfos[$gid]))
            {
                $resData['type3']['coin'] += $num * $giftInfos[$gid]['coin'];
                $resData['type3']['desc'] .= "{$giftInfos[$gid]['name']}x{$num}、";
            }
        }

        $resData['type1'] = rtrim($resData['type1'], '、');
        $resData['type2']['desc'] = rtrim($resData['type2']['desc'], '、');
        $resData['type3']['desc'] = rtrim($resData['type3']['desc'], '、');

        return $resData;
    }

    /**
     * 获取直播间礼物赠送数据
     * @param $liveId
     * @param $startTime
     * @param $endTIme
     * @return array
     */
    public function getTimeRangeGiftData($liveId, $startTime, $endTIme)
    {
        $sendGiftData = $this->where('live_id', $liveId)->where('create_time', 'between time', [$startTime, $endTIme])->group('gid')->column('sum(send_num) num', 'gid');

        $giftIds = array_keys($sendGiftData);

        $giftInfos = (new LiveGift())->whereIn('id', $giftIds)->column('type,name,coin', 'id');

        $resData = [
            'type1' => '',
            'type2' => [
                'coin' => 0,
                'desc' => ''
            ],
            'type3' => [
                'coin' => 0,
                'desc' => ''
            ],
        ];
        foreach ($sendGiftData as $gid => $num)
        {
            if (isset($giftInfos[$gid]))
            {
                if ($giftInfos[$gid]['type'] == 1)
                {   // 免费礼物
                    $resData['type1'] .= "{$giftInfos[$gid]['name']}x{$num}、";
                } else
                {   // 付费礼物

                    $resData['type2']['coin'] += $num * $giftInfos[$gid]['coin'];
                    $resData['type2']['desc'] .= "{$giftInfos[$gid]['name']}x{$num}、";
                }
            }
        }

        $BagSendGiftData = $this->alias('r')->join('wechat_backup_07_18_14_43_00.live_gift g', 'r.gid=g.id')->where('r.live_id', $liveId)->where('r.create_time', 'between time', [$startTime, $endTIme])
            ->where('g.type', 2)->where('r.type', 2)->group('r.gid')->column('sum(r.send_num) num', 'r.gid');

        foreach ($BagSendGiftData as $gid => $num)
        {
            if (isset($giftInfos[$gid]))
            {
                $resData['type3']['coin'] += $num * $giftInfos[$gid]['coin'];
                $resData['type3']['desc'] .= "{$giftInfos[$gid]['name']}x{$num}、";
            }
        }

        $resData['type1'] = rtrim($resData['type1'], '、');
        $resData['type2']['desc'] = rtrim($resData['type2']['desc'], '、');
        $resData['type3']['desc'] = rtrim($resData['type3']['desc'], '、');

        return $resData;
    }
}
